/*=============== This converts reg estimates to csv file to use in R =================*/
 use "${data1}regready_cps_ipums_84.dta" , clear
local index = 0
local j = 1
foreach d of numlist 1(1)8 {
foreach spec of numlist 1 3 {
foreach year of numlist 2014(1)2020 {

 local index = `index'+1
 preserve
 
 cap drop under
 if `spec' < 3 gen under = contpov_dube < `j'
 else gen under = contpov < `j'
 sum under [aw=asecwt] if dem`d'==1 & year<=`year'
 local DVmean=   `r(mean)'
 di `DVmean'

  est use "${est}ster/other/MW1_Yearly_`year'_`spec'_`d'"
  lincom lnMW 
  local beta1 = `r(estimate)'
  local sd1 = `r(se)'
  
  est use "${est}ster/other/MW2_Yearly_`year'_`spec'_`d'"
  lincom lnMW + lnMWL1 + lnMWL2 + lnMWL3
  local beta2 = `r(estimate)'
  local sd2 = `r(se)'
  
  
  clear
  set obs 1 
  gen beta1 = `beta1'
  gen sd1 = `sd1'
  gen beta2 = `beta2'
  gen sd2 = `sd2'
  gen mean = `DVmean'
  gen d = `d'
  gen spec = `spec'
  gen year = `year'
  
  tempfile a`index'
  save `a`index''
  
  dis `index'
  restore
  }
}
}

clear
foreach i of numlist 1(1)`index' {
 append using `a`i''
}
save "${est}ster/Sensitivity_Yearly_Demographics", replace










************** Dube PERIOD ***************
use  "${data1}march_regready_1984.dta", clear
gen eitc_raw = eitc
gen byte demgroup_lowskill = demgroup26==1 & inrange(age, 16,24) 
gen byte demgroup_workingage = inrange(age, 16, 64)
gen byte demgroup_children = age<16
ren (demgroup0 demgroup_workingage demgroup27  demgroup_lowskill demgroup21 demgroup24  demgroup_children) ///
(demgroup2 demgroup3 demgroup4 demgroup5 demgroup6 demgroup7 demgroup8)


local j = 1

local index = 0
foreach spec of numlist 1 2 4 {
foreach d of numlist 1(1)8 {
local q = `j' * 100
  
  local index = `index'+1
  
  preserve
  
  cap drop under
  if `spec'==4 gen under = contpov < `j'
  else gen under = contpov2 < `j'

  sum under [aw=wtsupp] if demgroup`d'==1
  local DVmean=   `r(mean)'
   di `DVmean'
  
  est use "${est}ster/dube/MW1_`q'_dem`d'_s`spec'_dube"
  * di `spec'
  lincom lnMW 
  local beta1 = `r(estimate)'
  local sd1 = `r(se)'
  
  est use "${est}ster/dube/MW2_`q'_dem`d'_s`spec'_dube"
  lincom lnMW + lnMWL1 + lnMWL2 + lnMWL3
  local beta2 = `r(estimate)'
  local sd2 = `r(se)'
  
  
  clear
  set obs 1 
  gen beta1 = `beta1'
  gen sd1 = `sd1'
  gen beta2 = `beta2'
  gen sd2 = `sd2'
  gen mean = `DVmean'
  gen d = `d'
  gen spec = `spec'
  
  tempfile a`index'
  save `a`index''
  
  dis `index'
  restore
  }
}

clear
foreach i of numlist 1(1)`index' {
 append using `a`i''
}
save "${est}ster/Dube_Demographics", replace
	











